package com.yami.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.dto.SysMsgDTO;
import com.yami.shop.bean.model.SysMsg;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.util.PageUtil;
import com.yami.shop.dao.SysMsgMapper;
import com.yami.shop.service.SysMsgService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SysMsgServiceImpl extends ServiceImpl<SysMsgMapper, SysMsg> implements SysMsgService {


    /**
     * 发布系统消息
     *
     * @param sysMsgDTO
     */
    @Override
    public void publishSysMessage(SysMsgDTO sysMsgDTO) {
        SysMsg sysMsg = BeanUtil.copyProperties(sysMsgDTO, SysMsg.class);
        this.saveOrUpdate(sysMsg);
    }

    /**
     * 系统消息列表
     *
     * @return
     */
    @Override
    public List<SysMsg> list() {
        LambdaQueryWrapper<SysMsg> queryWrapper = new LambdaQueryWrapper<SysMsg>().eq(SysMsg::getStatus, "0")
                .eq(SysMsg::getDelFlag, "0");
        return super.list(queryWrapper);
    }

    /**
     * 系统消息分页
     *
     * @param pageDTO
     * @return
     */
    @Override
    public PageVO<SysMsg> page(PageDTO pageDTO) {
        LambdaQueryWrapper<SysMsg> queryWrapper = new LambdaQueryWrapper<SysMsg>()
                .eq(SysMsg::getStatus, "0")
                .eq(SysMsg::getDelFlag, "0");
        PageVO<SysMsg> doPage = PageUtil.doPage(pageDTO, () -> this.list(queryWrapper));
        return doPage;
    }

    /**
     * 删除公告
     *
     * @param ids
     */
    @Override
    public void removeIds(List<Long> ids) {
        List<SysMsg> msgs = Lists.newArrayList();
        for (Long id : ids) {
            SysMsg sysMsg = new SysMsg(id);
            sysMsg.setDelFlag("1");
            msgs.add(sysMsg);
        }
        this.updateBatchById(msgs);
    }
}
